#cmake file for common_source_cpp
#author:guyadong
#created:2018/04/24
##! 用于velrelcm.sh 的 变量搜索正则表达式, 不要轻易修改
##! @PRJNAME_REG@          (set\s*\(\s*COMMON_SOURCE_CPP_PRJNAME\s+)([[:alnum:]_]+)(\s*\))
##! @VERSION_REG@          (set\s*\(\s*COMMON_SOURCE_CPP_VERSION\s+)(\S*)(.*\))
##! @SNAPSHOT_REG@         (set\s*\(\s*COMMON_SOURCE_CPP_VERSION\s+[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)(-[[:alnum:]]+)?(.*\))
cmake_minimum_required( VERSION 3.0 )
set(COMMON_SOURCE_CPP_PRJNAME common_source_cpp)
set(COMMON_SOURCE_CPP_VERSION 1.0.3-DEV)
message(STATUS ${COMMON_SOURCE_CPP_PRJNAME}-${COMMON_SOURCE_CPP_VERSION})
# fix cmake warning
if(POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
endif()
option(COMMON_SOURCE_CPP_COPY "Copy source to dependent dir" ON)

if(COMMON_SOURCE_CPP_COPY)
	# 将 common_source_cpp复制到依赖代码文件夹
	if(NOT DEPENDENT_SOURCE_DIR)
		set(DEPENDENT_SOURCE_DIR "${PROJECT_BINARY_DIR}/dependent_sources" CACHE STRING "dependent source folder" FORCE)
	endif()
	file(GLOB _common_source_files "${CMAKE_CURRENT_LIST_DIR}/*")
	file(COPY ${_common_source_files} 
		DESTINATION ${DEPENDENT_SOURCE_DIR} 
		FILES_MATCHING
		PATTERN "*.h"
		PATTERN "*.cpp"
		PATTERN "*.hpp"
		PATTERN "*.hh"
		PATTERN "*.c"
		)
	message( STATUS "copy files of ${CMAKE_CURRENT_LIST_DIR} to ${DEPENDENT_SOURCE_DIR}" )
	unset(_common_source_files)
else()
	set(DEPENDENT_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR} CACHE STRING "dependent source folder" FORCE)
	message( STATUS "DEPENDENT_SOURCE_DIR=${DEPENDENT_SOURCE_DIR}" )
endif()

add_library(common_source INTERFACE)
target_include_directories(common_source INTERFACE
  $<BUILD_INTERFACE:${DEPENDENT_SOURCE_DIR}>
  $<INSTALL_INTERFACE:include>
)
if(MSVC)
	# MSVC需要dirent.h支持
	target_include_directories(common_source INTERFACE
		$<BUILD_INTERFACE:${DEPENDENT_SOURCE_DIR}/dirent>
		$<INSTALL_INTERFACE:include/dirent>
		)
	message(STATUS "INCLUDE dirent.h")
endif()

add_library(common_source_cpp::common_source_cpp ALIAS common_source)